package com.hyjx.framework.component.log;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.orm.dao.IBaseDao;
import com.hyjx.framework.orm.dao.Pager;
import com.hyjx.framework.service.ConfigManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务日志实现类
 */
@Service
@Transactional
public class LogBusinessImpl implements LogBusiness {
    Logger log = LoggerFactory.getLogger(LogBusinessImpl.class);
    @Resource
    private IBaseDao dao;

    @Override
    public String queryList(LogBusinessModel model) {
        Pager pager = dao.getPager(model.getModuleName() + ".queryList", model.getModuleName() + ".queryCount", model);
        return CommonUtil.getPageJsonStr(pager);
    }

    @Override
    public boolean add(LogBusinessModel model) {
        return dao.addObject(model.getModuleName() + ".insert", model);
    }

    @Override
    public LogBusinessModel getInfoById(String s) {
        LogBusinessModel model = null;
        model = (LogBusinessModel) dao.queryObject(new LogBusinessModel().getModuleName() + ".getInfoById", s);
        return model;
    }

    @Override
    public int edit(LogBusinessModel model) {
        return dao.updateObject(model.getModuleName() + ".update", model);
    }

    @Override
    public int delete(String ids) {
        String[] id_array = ids.split(",");
        int num = 0;
        for (String id : id_array) {
            dao.deleteObject(new LogBusinessModel().getModuleName() + ".delete", id);
            num++;
        }
        return num;
    }

    @Override
    public String deleteMonthData(String ids) {
        
        int num = dao.deleteObject(new LogBusinessModel().getModuleName() + ".deleteMonthData", ids);
        if (num >= 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @Override
    public String queryfwtj(LogBusinessModel model) {


        return null;
    }

    public int getLogBufferSize() {
        String logbuffersize = ConfigManager.getConfig("logIntereptor.buffersize");
        if (logbuffersize == null || logbuffersize.trim().length() == 0) {
            logbuffersize = "1000";
        }
        int logbuffersizeint = 1000;
        try {
            logbuffersizeint = Integer.parseInt(logbuffersize);
        } catch (NumberFormatException e) {
            log.error("logIntereptor.buffersize must be int.");
        }
        return logbuffersizeint;
    }

    @Override
    public boolean addDataLog(LogBusinessDataModel model) {
        return dao.addObject(model.getModuleName() + ".insert", model);
    }

    @Override
    public String queryDataList(LogBusinessDataModel model) {
        Pager pager = dao.getPager(model.getModuleName() + ".queryList", model.getModuleName() + ".queryCount", model);
        return CommonUtil.getPageJsonStr(pager);
    }

    @Override
    public List<LogBusinessDataModel> getLogDataModel(LogBusinessDataModel model) {
        return dao.queryObjectList(model.getModuleName() + ".queryList", model);
    }
    
    @Override
    public void logbusinessdata(LogBusinessDataModel model) {
        dao.addObject(model.getModuleName() + ".insert", model);
    }

    @Override
    public void logbusinessdataupdate(LogBusinessDataModel model) {
        dao.updateObject(model.getModuleName() + ".update", model);

    }

}
